package HandleXml.service;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.jdom2.CDATA;
import org.jdom2.Namespace;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class BookWriteService {
    private static final String OUTPUT_DOM4J = "src/main/resources/output_dom4j.xml";
    private static final String OUTPUT_JDOM = "src/main/resources/output_jdom.xml";

    @Test
    public void testDom4j(){
        writeWithDom4j();
    }
    @Test
    public void testJdom(){
        writeWithJdom();
    }

    // 使用dom4j写入XML
    public void writeWithDom4j() {
        try {
            // 创建Document对象
            Document document = DocumentHelper.createDocument();
            
            // 创建根元素和命名空间
            Element root = document.addElement("library")
                .addNamespace("lib", "http://www.library.org")
                .addNamespace("pub", "http://www.publisher.org");

            // 添加注释
            root.addComment("这是一个图书馆的藏书信息");

            // 创建第一本书
            Element book1 = root.addElement("book")
                .addAttribute("id", "101")
                .addAttribute("category", "科幻");

            book1.addElement("title")
                .addAttribute("lang", "zh")
                .setText("三体");

            Element author1 = book1.addElement("author");
            author1.addElement("name").setText("刘慈欣");
            author1.addElement("nationality").setText("中国");

            // 使用命名空间的元素
            Element publisher1 = book1.addElement("pub:publisher");
            publisher1.addElement("pub:name").setText("重庆出版社");
            publisher1.addElement("pub:address").setText("重庆市渝中区");

            // 添加CDATA内容
            book1.addElement("description")
                .addCDATA("这是一部将宇宙文明\n        和人类文明相结合的科幻小说。");

            // 创建统计信息（使用命名空间）
            Element stats = root.addElement("lib:statistics");
            stats.addElement("lib:total_books").setText("1");
            stats.addElement("lib:avg_rating").setText("4.9");

            // 写入文件
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = new XMLWriter(new FileOutputStream(OUTPUT_DOM4J), format);
            writer.write(document);
            writer.close();

            System.out.println("DOM4J XML文件已创建: " + OUTPUT_DOM4J);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 使用JDOM2写入XML
    public void writeWithJdom() {
        try {
            // 创建Document对象
            org.jdom2.Document document = new org.jdom2.Document();
            
            // 创建命名空间
            Namespace libNs = Namespace.getNamespace("lib", "http://www.library.org");
            Namespace pubNs = Namespace.getNamespace("pub", "http://www.publisher.org");
            
            // 创建根元素
            org.jdom2.Element root = new org.jdom2.Element("library");
            root.addNamespaceDeclaration(libNs);
            root.addNamespaceDeclaration(pubNs);
            document.setRootElement(root);

            // 添加注释
            root.addContent(new org.jdom2.Comment("这是一个图书馆的藏书信息"));

            // 创建第一本书
            org.jdom2.Element book1 = new org.jdom2.Element("book");
            book1.setAttribute("id", "201");
            book1.setAttribute("category", "小说");

            org.jdom2.Element title1 = new org.jdom2.Element("title");
            title1.setAttribute("lang", "zh");
            title1.setText("活着");
            book1.addContent(title1);

            org.jdom2.Element author1 = new org.jdom2.Element("author");
            author1.addContent(new org.jdom2.Element("name").setText("余华"));
            author1.addContent(new org.jdom2.Element("nationality").setText("中国"));
            book1.addContent(author1);

            // 使用命名空间的元素
            org.jdom2.Element publisher1 = new org.jdom2.Element("publisher", pubNs);
            publisher1.addContent(new org.jdom2.Element("name", pubNs).setText("作家出版社"));
            publisher1.addContent(new org.jdom2.Element("address", pubNs).setText("北京市东城区"));
            book1.addContent(publisher1);

            // 添加CDATA内容
            org.jdom2.Element desc1 = new org.jdom2.Element("description");
            desc1.addContent(new CDATA("这是一部展现中国当代历史的\n        人性史诗。"));
            book1.addContent(desc1);

            root.addContent(book1);

            // 创建统计信息（使用命名空间）
            org.jdom2.Element stats = new org.jdom2.Element("statistics", libNs);
            stats.addContent(new org.jdom2.Element("total_books", libNs).setText("1"));
            stats.addContent(new org.jdom2.Element("avg_rating", libNs).setText("4.8"));
            root.addContent(stats);

            // 写入文件
            XMLOutputter xmlOutput = new XMLOutputter();
            xmlOutput.setFormat(Format.getPrettyFormat());
            xmlOutput.output(document, new FileOutputStream(OUTPUT_JDOM));

            System.out.println("JDOM2 XML文件已创建: " + OUTPUT_JDOM);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
